<?php
class my extends base{

	public function init(){
		parent::init();

		$this->current = 'my';
		$this->tit = '我的';

		$this->modelObj = D('company_admin');

		$this->orderObj = D('company_order');

		$this->shopObj = D('company_shop');

		$this->score_logObj = D('company_score_log');
		$this->money_logObj = D('company_money_log');
		$this->cash_logObj = D('company_cash_log');

		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');
		$this->deviceObj = D('company_device');
		$this->adminObj = D('company_admin');

		$this->helpObj = D('company_help');
		
		// $this->smsObj = D('sms');

		$this->auth_list = $this->adminObj->get_auth_list($this->admin);
		$this->areas = $this->auth_list['areas'];
		$this->cates = $this->auth_list['cates'];
		$this->zones = $this->auth_list['zones'];
		
	}

	public function index(){

		$cond = "status=1 and parent_id=0";
		$this->cates = $this->cateObj->findAll(array($cond));

		$cond = "status=1 ".$this->cond_base;

		//权限
		// switch($this->admin['level']){
		// 	case '0'://管理员
		// 		$cond .=" ";
		// 		break;
		// 	case '1'://报修员
		// 		$cond .=" and admin_id={$this->admin_id}";
		// 		break;
		// 	case '2'://报修组长
		// 		$cond .=" ".$this->cond_cate.$this->cond_zone;
		// 		break;
		// 	case '3'://维修师傅
		// 		$cond .=" and shifu_id={$this->admin_id}";
		// 		break;
		// 	case '4'://维修组长
		// 		$cond .=" ".$this->cond_cate.$this->cond_zone;
		// 		break;
		// }

		$this->cond = $cond;

		$cond = $this->cond . " and admin_id={$this->admin_id} and order_status<99";
		$stat['order1'] = $this->orderObj->findCount(array($cond));
		
		$cond = $this->cond . " and shifu_id={$this->admin_id} and order_status<99";
		$stat['order2'] = $this->orderObj->findCount(array($cond));

		// $cond = $this->cond . " and order_status=3";
		// $stat['order3'] = $this->orderObj->findCount(array($cond));

		// $cond = $this->cond . " and order_status=4";
		// $stat['order4'] = $this->orderObj->findCount(array($cond));

		// $cond = $this->cond . " and order_status=5";
		// $stat['order5'] = $this->orderObj->findCount(array($cond));

		

		$this->stat = $stat;
	}

	public function setting(){

		if($this->fmdo == 'setting'){
			$arrBase = array(
				'face' =>arg('face'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}

		if($this->fmdo == 'sex'){
			$arrBase = array(
				'sex' =>arg('sex'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}

		if($this->fmdo == 'sign'){
			$arrBase = array(
				'is_online' =>arg('is_online'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}

		if($this->fmdo == 'map'){
			$arrBase = array(
				'lng' =>arg('lng'),
				'lat' =>arg('lat'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('定位成功');
		}
	}

	public function pwd(){

		if($this->fmdo == 'pwd'){
			$arrBase = array(
				'password' => qcing_hash(arg('pwd')),
			);
			$this->modelObj->edit($this->user_id, $arrBase);
			$this->renderSuccess('修改成功');
		}
	}

	public function stat(){

		$cond = "status=1 ".$this->cond_base;


		//权限
		switch($this->user['level']){
			case '0'://管理员
				$cond .=" ";
				break;
			case '1'://报修员
				$cond .=" and admin_id={$this->user_id}";
				break;
			case '2'://报修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
			case '3'://维修师傅
				$cond .=" and shifu_id={$this->user_id}";
				break;
			case '4'://维修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
		}
		//总订单
		$cond_sql0 = $cond . "";

		//今日订单
		$day1 = date("Y-m-d");
		$cond_sql1 = $cond . " and (add_time>='{$day1}')";

		//昨日订单
		$day1 = date("Y-m-d");
		$day2 = date("Y-m-d",strtotime("-1 day"));
		$cond_sql2 = $cond . " and (add_time>='{$day1}' and add_time<='{$day2}')";
		//本周订单
		// $day1 = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
		// $cond_sql2 = $cond . " and (add_time>='{$day1}')";
		//本月订单
		$day1 = date("Y-m-01");
		$cond_sql3 = $cond . " and (add_time>='{$day1}')";
		//上月订单
		$day1 = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
		$day2 = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);
		$cond_sql4 = $cond . " and (add_time>='{$day1}' and add_time<='{$day2}')";
		
		//统计
		$stat['order0'] = $this->orderObj->findCount(array($cond_sql0));
		$stat['order1'] = $this->orderObj->findCount(array($cond_sql1));
		$stat['order2'] = $this->orderObj->findCount(array($cond_sql2));
		$stat['order3'] = $this->orderObj->findCount(array($cond_sql3));
		$stat['order4'] = $this->orderObj->findCount(array($cond_sql4));

		// //权限
		// switch($this->user['level']){
		// 	case '10'://管理员
		// 		$cond_yj = 'sum(yj_money1) as yj_money2';
		// 		break;
		// 	case '1'://
		// 		$cond_yj = 'sum(yj_money2) as yj_money2';
		// 		break;
		// 	case '3'://
		// 		$cond_yj = 'sum(yj_money1) as yj_money2';
		// 		break;
		// 	case '4':// 
		// 		$cond_yj = 'sum(yj_money1) as yj_money2';
		// 		break;
		// }

		// $stat['money'] = $this->orderObj->count_money($cond_sql,$cond_yj);
		// $stat['money1'] = $this->orderObj->count_money($cond_sql1,$cond_yj);
		// $stat['money2'] = $this->orderObj->count_money($cond_sql2,$cond_yj);
		// $stat['money3'] = $this->orderObj->count_money($cond_sql3,$cond_yj);
		// $stat['money4'] = $this->orderObj->count_money($cond_sql4,$cond_yj);
		$this->stat = $stat;

	}

	public function sign(){
		if($this->fmdo == 'sign'){
			$arrBase = array(
				'is_sign' =>arg('sign'),
			);
			$this->modelObj->edit($this->admin_id, $arrBase);
			$this->renderSuccess('设置成功');
		}
	}

	//vip
	public function vip(){

		if($this->fmdo == 'vip'){
			$fee = arg('fee');
			$fee = 100;
			
			if($this->admin['money'] < $fee ){
				$this->renderError('账户余额不足'.$fee);
			}
			//vip
			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'type' => 0,
				'money' => - $fee,
				'msg' => '升级VIP',
			);
			$this->money_logObj->add_log($arrBase);
			$this->modelObj->add_money($this->admin_id, - $fee);


			if($this->admin['vip_time'] < date('Y-m-d')){
				$vip_time = date('Y-m-d',strtotime('+90 day'));
			}else{
				$vip_time = date('Y-m-d', strtotime ("+90 day", strtotime($this->admin['vip_time'])));
			}

			$arrBase = array(
				'is_vip' =>1,
				'vip_time' =>$vip_time,
			);
			$this->modelObj->edit($this->admin_id, $arrBase);

			$this->renderSuccess('升级成功');

		}
	}


	//提现
	public function getcash(){

		if($this->fmdo == 'getcash'){
			$fee = arg('fee');
			// if(!$this->user['bank_card']){
			// 	$this->ajaxMsg(0,'请先设置收款信息','/index/my/bank');
			// }
			if($fee < 100 ){
				$this->renderError('提现金额小于100');
			}
			if($this->admin['money'] < $fee ){
				$this->renderError('账户佣金余额不足');
			}
			//提现
			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'type' => 0,
				'money' => - $fee,
				'msg' => '余额提现',
			);
			$this->money_logObj->add_log($arrBase);
			$this->modelObj->add_money($this->admin_id, - $fee);

			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'type' => 0,
				'money' => - $fee * 0.95,
				'msg' => "余额提现",
				'status'=> 0,
			);
			$this->cash_logObj->add_log($arrBase);

			$this->renderSuccess('申请成功');

		}
	}

	public function cash_log(){
		$this->tab = $this->tab?$this->tab:'1';
		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,$this->action.'_loaddata', $arrBase);
		
	}

	//loaddata
	public function cash_log_loaddata(){
		
		$cond = "1=1 and admin_id={$this->admin_id}";

		switch($this->tab){
			case '1'://管理员
				$cond .= ' and status=0';
				break;
			case '2'://
				$cond .= ' and status=1';
				break;
		}

		//搜索
		if($this->keyword){
			$cond.=" and (msg like '%".$this->keyword."%' ) ";
		}
		
		$result = $this->cash_logObj->get_loaddata($cond);
		if($result){
			$this->list = $result['list'];
		}else{
			die('0');
		}
	}


	//点数
	public function getscore(){

		if($this->fmdo == 'getscore'){
			$fee = arg('fee');
			// if(!$this->user['bank_card']){
			// 	$this->ajaxMsg(0,'请先设置收款信息','/index/my/bank');
			// }
		
			if($fee < 10 ){
				$this->renderError('充值金额小于10');
			}
			if($this->admin['money'] < $fee ){
				$this->renderError('账户余额不足');
			}

			// 点数
			$score = $fee * 10;

			//减少金额
			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'type' => 0,
				'money' => - $fee,
				'msg' => '点数充值减少',
			);
			$this->money_logObj->add_log($arrBase);
			$this->adminObj->add_money($this->admin_id, - $fee);

			
			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'type' => 0,
				'score' => $score,
				'msg' => "余额充值增加",
				'status'=> 1,
			);
			$this->score_logObj->add_log($arrBase);
			$this->adminObj->add_score($this->admin_id, $score);

			$this->renderSuccess('兑换成功');

		}
	}



	public function score_log(){
		$this->tab = $this->tab?$this->tab:'1';
		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,$this->action.'_loaddata', $arrBase);
		
	}

	//loaddata
	public function score_log_loaddata(){
		
		$cond = "status=1 and admin_id={$this->admin_id}";

		switch($this->tab){
			case '1'://管理员
				$cond .= ' and score>0';
				break;
			case '2'://
				$cond .= ' and score<0';
				break;
		}

		//搜索
		if($this->keyword){
			$cond.=" and (msg like '%".$this->keyword."%' ) ";
		}

		$result = $this->score_logObj->get_loaddata($cond);
		// dump($result);exit;
		if($result){
			$this->list = $result['list'];
		}else{
			die('0');
		}
	}

	public function money_log(){
		$this->tab = $this->tab?$this->tab:'1';
		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,$this->action.'_loaddata', $arrBase);
		
	}

	//loaddata
	public function money_log_loaddata(){
		
		$cond = "status=1 and admin_id={$this->admin_id}";

		switch($this->tab){
			case '1'://管理员
				$cond .= ' and money>0';
				break;
			case '2'://
				$cond .= ' and money<0';
				break;
		}

		//搜索
		if($this->keyword){
			$cond.=" and (msg like '%".$this->keyword."%' ) ";
		}

		$result = $this->money_logObj->get_loaddata($cond);
		// dump($result);exit;
		if($result){
			$this->list = $result['list'];
		}else{
			die('0');
		}
	}


	public function help(){
		$this->tab = $this->tab?$this->tab:'1';
		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,$this->action.'_loaddata', $arrBase);
		
	}

	//loaddata
	public function help_loaddata(){
		
		$cond = "status=1";

		// switch($this->tab){
		// 	case '1'://管理员
		// 		$cond .= ' and money>0';
		// 		break;
		// 	case '2'://
		// 		$cond .= ' and money<0';
		// 		break;
		// }

		//搜索
		if($this->keyword){
			$cond.=" and (title like '%".$this->keyword."%' ) ";
		}

		$result = $this->helpObj->get_loaddata($cond);
		// dump($result);exit;
		if($result){
			$this->list = $result['list'];
		}else{
			die('0');
		}
	}




}